package com.haiyou.qrcode.service.impl;

import com.haiyou.qrcode.poster.drawable.*;
import com.haiyou.qrcode.poster.drawable.Image;
import com.haiyou.qrcode.poster.kernal.ColorTools;
import com.haiyou.qrcode.poster.kernal.Drawable;
import com.haiyou.qrcode.service.PosterService;
import org.springframework.util.DigestUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class PosterServiceImpl implements PosterService {

    /**
     * 绘制图片
     *
     * @return File
     * @throws IOException
     */
    @Override
    public File draw(Poster poster) throws IOException {

        String format = poster.getFormat();
        Integer width = poster.getWidth();
        Integer height = poster.getHeight();

        // 初始化图片
        BufferedImage image = new BufferedImage(width, height, "png".equals(format) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_3BYTE_BGR);

        // create graphics
        Graphics2D gd = image.createGraphics();

        // 初始化画布层级 map
        Map<Integer, ArrayList<Drawable>> indexMap = new HashMap<>();
        ArrayList<Drawable> drawables;

        // 如果有背景，画个矩形做背景
        if (poster.getBackgroundColor() != null) {
            gd.setColor(ColorTools.String2Color(poster.getBackgroundColor()));
            gd.fillRect(0, 0, width, height);
        }

        ArrayList<Block> blocks = poster.getBlocks();
        if (blocks != null && blocks.size() > 0) {
            // 遍历 blocks
            for (Block block : blocks) {
                push2map(indexMap, block);
            }
        }

        ArrayList<Line> lines = poster.getLines();
        if (lines != null && lines.size() > 0) {
            // 遍历 lines
            for (Line line : lines) {
                push2map(indexMap, line);
            }
        }

        ArrayList<Image> images = poster.getImages();
        if (images != null && images.size() > 0) {
            // 遍历 images
            for (Image img : images) {
                push2map(indexMap, img);
            }
        }

        ArrayList<Text> texts = poster.getTexts();
        if (texts != null && texts.size() > 0) {
            // 遍历 texts
            for (Text text : texts) {
                push2map(indexMap, text);
            }
        }

        // 按 index 顺序执行绘画过程
        for (Integer index : indexMap.keySet()) {
            drawables = indexMap.get(index);
            if (drawables != null) {
                for (Drawable drawable : drawables) {
                    drawable.draw(gd, width, height);
                }
            }
        }

        gd.dispose();

        // 创建临时文件
        File file = File.createTempFile(this.key(), "." + format);
        // 把文件写入图片
        ImageIO.write(image, format, file);
        // 使用完后删除文件
        file.deleteOnExit();
        return file;
    }

    private void push2map(Map<Integer, ArrayList<Drawable>> indexMap, Drawable drawable) {
        ArrayList<Drawable> drawables = indexMap.get(drawable.getZIndex());
        drawables = drawables == null ? new ArrayList<>() : drawables;
        drawables.add(drawable);
        indexMap.put(drawable.getZIndex(), drawables);
    }

    /**
     * 获取key
     *
     * @return String
     */
    public String key() {

        return DigestUtils.md5DigestAsHex(String.valueOf(new Date().getTime()).getBytes());
    }
}
